﻿using System;
using System.Xml;

namespace PluginRegistrationTool
{
    class Program
    {
        static void Main(string[] args)
        {
            RegistrationHelper registrationHelper = new RegistrationHelper();
            try
            {
                string path = "AssemblyRegistration.xml";
                bool unregister = false;
                bool register = true;
                foreach (string arg in args)
                {
                    if (arg.Split('=').GetValue(0).ToString().ToUpperInvariant() == "/PATH")
                    {
                        path = arg.Split('=').GetValue(1).ToString();
                    }
                    if (arg.ToUpperInvariant() == "/UNREGISTER")
                    {
                        unregister = true;
                    }
                    if (arg.ToUpperInvariant() == "/?" || arg == "/HELP")
                    {
                        Console.WriteLine(Properties.Resources.title);
                        Console.WriteLine();
                        Console.WriteLine(Properties.Resources.descriptionArrayOfAssemblies);
                        Console.WriteLine(Properties.Resources.descriptionAssembly);
                        Console.WriteLine(Properties.Resources.descriptionPlugin);
                        Console.WriteLine(Properties.Resources.descriptionStep);
                        Console.WriteLine();
                        Console.WriteLine(Properties.Resources.switches);
                        Console.WriteLine();
                        Console.WriteLine(Properties.Resources.pathSwitch);
                        Console.WriteLine();
                        Console.WriteLine(Properties.Resources.unregisterSwitch);
                        Console.WriteLine();
                        Console.WriteLine(Properties.Resources.helpSwitch);
                        Console.WriteLine();
                        Console.WriteLine();
                        Console.WriteLine(Properties.Resources.examples);
                        Console.WriteLine();
                        Console.WriteLine(Properties.Resources.helpExample1);
                        Console.WriteLine();
                        Console.WriteLine(Properties.Resources.helpExample2);
                        Console.WriteLine();
                        Console.WriteLine(Properties.Resources.noParametersExample);
                        Console.WriteLine();
                        Console.WriteLine(Properties.Resources.unregisterExample);
                        Console.WriteLine();
                        Console.WriteLine(Properties.Resources.pathExample);
                        Console.WriteLine();
                        Console.WriteLine(Properties.Resources.pathUnregisterExample);
                        register = false;
                    }
                }
                if (register)
                {
                    XmlDocument assemblyRegistrationXml = new XmlDocument();
                    assemblyRegistrationXml.Load(path);
                    if (!unregister)
                    {
                        Console.WriteLine(Properties.Resources.registeringAssembliesNotification);
                        registrationHelper.Register(assemblyRegistrationXml);
                    }
                    else
                    {
                        Console.WriteLine(Properties.Resources.unregisteringAssembliesNotification);
                        registrationHelper.Unregister(assemblyRegistrationXml);
                    }
                    if (registrationHelper.Errors.Count != 0)
                    {
                        foreach (string error in registrationHelper.Errors)
                        {
                            Console.WriteLine(error);
                            Console.WriteLine();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                if (registrationHelper.Errors.Count != 0)
                {
                    foreach (string error in registrationHelper.Errors)
                    {
                        Console.WriteLine(error);
                        Console.WriteLine();
                    }
                }
                Console.WriteLine(Properties.Resources.exceptionOccurred);
                Console.WriteLine();
                Console.WriteLine(Properties.Resources.errorMessage);
                Console.WriteLine(e.Message);
                Console.WriteLine(Properties.Resources.errorSource);
                Console.WriteLine(e.Source);
                Console.WriteLine(Properties.Resources.errorStackTrace);
                Console.WriteLine(e.StackTrace);
                Console.WriteLine(Properties.Resources.errorHelpLink);
                Console.WriteLine(e.HelpLink);
                Console.WriteLine(Properties.Resources.errorInnerExceptionMessage);
                if (e.InnerException != null)
                {
                    Console.WriteLine(e.InnerException.Message);
                    Console.WriteLine(Properties.Resources.errorInnerExceptionSource);
                    Console.WriteLine(e.InnerException.Source);
                    Console.WriteLine(Properties.Resources.errorInnerExceptionStackTrace);
                    Console.WriteLine(e.InnerException.StackTrace);
                    Console.WriteLine(Properties.Resources.errorInnerExceptionHelpLink);
                    Console.WriteLine(e.InnerException.HelpLink);
                }
            }
        }
    }
}
